<?php

// 公共助手函数

if (!function_exists('__lang')) {
    /**
     * @param string $name 语言键
     * @param string $sign 插件标识
     * @return string
     */
    function __lang(string $name, string $sign = 'wwh'): string
    {
        //测试自动构建
        // 参数过滤
        $sign = preg_replace('/[^a-z0-9_-]/i', '', $sign);
        $allowed_langs = ['zh-cn', 'en'];
        $lang = (int)($_COOKIE['lang'] ?? 1) == 2 ? 'en' : 'zh-cn';
        $lang = in_array($lang, $allowed_langs) ? $lang : 'zh-cn';

        // 路径构建
        $file = ADDON_PATH . "{$sign}/lang/{$lang}.php";
        if (!is_file($file)) {
            $file = ROOT_PATH . "addons/{$sign}/lang/{$lang}.php";
        }

        // 安全加载
        if (is_file($file)) {
            $data = include $file;
            if (is_array($data)) {
                return htmlspecialchars($data[$name] ?? $name, ENT_QUOTES, 'UTF-8');
            }
        }

        return htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
    }
}
